package com.stsolutions.budgetcontroller;

import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.stsolutions.budgetcontroller.db.DaoObject;
import com.stsolutions.budgetcontroller.db.DataBaseActions;
import com.stsolutions.budgetcontroller.db.DatabaseHelper;
import com.stsolutions.budgetcontroller.model.data.CostBucket;
import com.stsolutions.budgetcontroller.util.FormatDate;

public class NewBudgetControllActivity extends
		OrmLiteBaseActivity<DatabaseHelper> implements OnClickListener,
		OnTouchListener {

	protected final String LOG_TAG = getClass().getSimpleName();

	private static final int DATE_DIALOG_ID_1 = 0;
	private static final int DATE_DIALOG_ID_2 = 1;

	private int dateDialogSet = -1;

	public int getDateDialogSet() {
		return dateDialogSet;
	}

	public void setDateDialogSet(int dateDialogSet) {
		this.dateDialogSet = dateDialogSet;
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.DATE, dayOfMonth);
			cal.set(Calendar.MONTH, monthOfYear);
			cal.set(Calendar.YEAR, year);
			int dateDialogSet = getDateDialogSet();
			Log.i(LOG_TAG, "setting date for object: " + dateDialogSet);
			String dateText = FormatDate.formatDate(cal.getTime());
			Log.i(LOG_TAG, "setting date to: " + dateText);

			switch (dateDialogSet) {
			case DATE_DIALOG_ID_1:
				startDateEditText.setText(dateText);
				break;
			case DATE_DIALOG_ID_2:
				endDateEditText.setText(dateText);
				break;

			default:
				setDateDialogSet(-1);
				break;

			}
		}
	};

	private Button addButton;
	private Button cancelButton;
	private EditText startDateEditText;
	private EditText endDateEditText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcostbucketcustomdialog);

		// Fields init
		addButton = (Button) findViewById(R.id.dialogButtonOK);
		cancelButton = (Button) findViewById(R.id.dialogButtonCancel);
		startDateEditText = (EditText) findViewById(R.id.costBucketControlStartDate);
		endDateEditText = (EditText) findViewById(R.id.costBucketControlEndDate);

		// Listeners settings
		addButton.setOnClickListener(this);
		cancelButton.setOnClickListener(this);
		startDateEditText.setOnTouchListener(this);
		endDateEditText.setOnTouchListener(this);

	}

	@Override
	public void onClick(View v) {
		Intent budgetControllerActivity = new Intent(this,
				BudgetControllerActivity.class);
		switch (v.getId()) {
		case R.id.dialogButtonOK: {			
			CostBucket costBucket = getCostBucket();
			getHelper().doDataBaseAction(DataBaseActions.ACTION_SAVE_ITEM,
					costBucket, null, DaoObject.COST_BUCKET);
			startActivity(budgetControllerActivity);
			break;
		}
		case R.id.dialogButtonCancel: {
			startActivity(budgetControllerActivity);
			break;
		}
		}

	}

	private CostBucket getCostBucket() {
		EditText availableBudgetTV = (EditText) findViewById(R.id.costBucketBudget);
		EditText startDateTV = (EditText) findViewById(R.id.costBucketControlStartDate);
		EditText endDateTV = (EditText) findViewById(R.id.costBucketControlEndDate);

		CostBucket costBucket = new CostBucket();

		Date startCostControl = FormatDate.getDateObject(startDateTV.getText()
				.toString());
		Date endCostControl = FormatDate.getDateObject(endDateTV.getText()
				.toString());
		Double totalBudgetPerBucket = Double.parseDouble(availableBudgetTV
				.getText().toString());

		costBucket.setEndCostControl(endCostControl);
		costBucket.setStartCostControl(startCostControl);
		costBucket.setTotalBudgetPerBucket(totalBudgetPerBucket);

		return costBucket;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.costBucketControlStartDate: {
			showDialog(DATE_DIALOG_ID_1);
			break;
		}
		case R.id.costBucketControlEndDate: {
			showDialog(DATE_DIALOG_ID_2);
			break;
		}
		default:
			break;
		}
		return false;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		setDateDialogSet(id);
		switch (id) {
		case DATE_DIALOG_ID_1: {
			Calendar cal = Calendar.getInstance();

			return new DatePickerDialog(this, mDateSetListener,
					cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
					cal.get(Calendar.DATE));
		}

		case DATE_DIALOG_ID_2: {
			Calendar cal = Calendar.getInstance();
			return new DatePickerDialog(this, mDateSetListener,
					cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
					cal.get(Calendar.DATE));
		}
		default:
			return null;
		}

	}
}
